#ifndef QXGAME_GAMETIMER_H
#define QXGAME_GAMETIMER_H

#include <atomic>
#include <functional>

class GameTimer {
public:
    GameTimer() = default;
    ~GameTimer() = default;

    void Restart()
    {
        passTime = 0;
        shoted = false;
    }

    void Pause()
    {
        paused = true;
    }

    void Resume()
    {
        paused = false;
    }

    void OnUpdate(double delta)
    {
        if (paused) {
            return;
        }
        passTime = passTime + delta;
        if (passTime >= waitTime) {
            bool canShot = (!oneShot || !shoted);
            shoted = true;
            if (canShot && onTimeout) {
                onTimeout();
            }
            passTime = passTime - waitTime;
        }
    }

public:
    std::atomic<bool> oneShot = false;  // 是否是单次触发
    double waitTime = 0.1;              // 等待的指定时间
    std::function<void()> onTimeout;    // 计时器到达时间后的回调函数

private:
    std::atomic<double> passTime = 0;   // 从计时器创建开始过去的时间
    std::atomic<bool> paused = false;   // 标注计数器是否暂停
    std::atomic<bool> shoted = false;   // 如果是单次触发，记录是否触发过
};


#endif //SURVIVALGAME_TIMER_H
